﻿using DinkToPdf;
using SOEI.TxmApi.Extend.Core;
using System;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;

namespace SOEI.Solution.Helper
{
    public class PDFHelper
    {
        #region 单例实现

        private PDFHelper() { }

        private static PDFHelper _This;

        public static PDFHelper This
        {
            get
            {
                if(_This == null)
                {
                    _This = new PDFHelper();
                }

                return _This;
            }
        }

        #endregion

        private SynchronizedConverter _convert;

        private SynchronizedConverter convert
        {
            get
            {
                if(_convert == null)
                {
                    var tools = new PdfTools();
                    _convert = new SynchronizedConverter(tools);
                }
                return _convert;
            }
        }

        private string GetHtmlContent(string pUrl)
        {
            WebClient client = new WebClient();
            client.Credentials = CredentialCache.DefaultCredentials;
            byte[] vPage = client.DownloadData(pUrl);
            return Encoding.UTF8.GetString(vPage);
        }

        public byte[] CreatePDF(string pHtml, bool pShowPager = false, string pPageFormat = null)
        {
            var globalSettings = new GlobalSettings
            {
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings
                {
                    Top = 20,
                    Left = 0,
                    Right = 0,
                    Bottom = 20,
                    Unit = Unit.Millimeters
                }
            };

            var objectSettings = new ObjectSettings
            {
                PagesCount = true,
                HtmlContent = pHtml,
                // Page = "www.baidu.com", //USE THIS PROPERTY TO GENERATE PDF CONTENT FROM AN HTML PAGE  这里是用现有的网页生成PDF
                //WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
                WebSettings = { 
                    DefaultEncoding = "utf-8",
                    PrintMediaType = true,
                    EnableJavascript = true,
                    EnableIntelligentShrinking = false
                },
                LoadSettings = {
                    DebugJavascript = true
                }
                //HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
                //FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }
            };

            if (pShowPager)
            {
                if(pPageFormat == null)
                {
                    pPageFormat = "Page [page] of [toPage]";
                }

                objectSettings.FooterSettings = new FooterSettings(){
                    FontName = "Arial", FontSize = 9, Line = true, Right = pPageFormat
                };
            }

            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings }
            };

            return convert.Convert(pdf);
        }

        
    }
}
